package com.syscom.admin.service;

import com.alibaba.fastjson.JSONObject;
import com.syscom.common.easyEntity.GenerateCodeBody;
import com.syscom.common.utils.TextUtil;
import com.syscom.common.utils.generator.java.AdminGenerator;
import com.syscom.common.utils.generator.java.CommonGenerator;
import com.syscom.common.utils.generator.vue.VueGenerator;
import com.syscom.common.utils.result.AjaxResult;
import com.syscom.common.utils.result.ResultTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wjz
 * @date 2020/11/22 0:46
 */
@Slf4j
@Service
public class SystemService {

    @Autowired
    private AdminGenerator adminGenerator;

    @Autowired
    private CommonGenerator commonGenerator;

    @Autowired
    private VueGenerator vueGenerator;

    public AjaxResult generateCode(GenerateCodeBody generateCodeBody) throws Exception {

        JSONObject queryObj = JSONObject.parseObject(generateCodeBody.getQuery());
        JSONObject tableHeadObj = JSONObject.parseObject(generateCodeBody.getTableHead());
        commonGenerator.commonCodeGenerator("common", "com.syscom", generateCodeBody.getTableName());
        Map map = adminGenerator.adminCodeGenerator("admin", "com.syscom", generateCodeBody.getTableName(), queryObj);
        Map dataModel = new HashMap() {{
            put("indexNameUpper", generateCodeBody.getName());
            put("indexName", TextUtil.firstToLowerCase(generateCodeBody.getName()));
            put("pagePath", generateCodeBody.getPath());
            put("queryNum", 24 / (queryObj.size() + 1));
            put("queryObj", queryObj);
            put("tableHeadObj", tableHeadObj);
            put("servicePath", "/services/" + generateCodeBody.getPath() + "/" + TextUtil.firstToLowerCase(generateCodeBody.getName()));
            putAll(map);
        }};
        vueGenerator.generator(dataModel, generateCodeBody.getPath(), generateCodeBody.getName());
        return ResultTool.success();
    }
}
